//
//  NewsDetailDetail.m
//
//  Created by lizhenqiang  on 16/4/10
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "NewsDetailDetail.h"
#import "NewsDetailContent.h"


NSString *const kNewsDetailDetailId = @"_id";
NSString *const kNewsDetailDetailTitle = @"title";
NSString *const kNewsDetailDetailContent = @"content";
NSString *const kNewsDetailDetailPublishTime = @"publish_time";


@interface NewsDetailDetail ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation NewsDetailDetail

@synthesize detailIdentifier = _detailIdentifier;
@synthesize title = _title;
@synthesize content = _content;
@synthesize publishTime = _publishTime;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.detailIdentifier = [self objectOrNilForKey:kNewsDetailDetailId fromDictionary:dict];
            self.title = [self objectOrNilForKey:kNewsDetailDetailTitle fromDictionary:dict];
    NSObject *receivedNewsDetailContent = [dict objectForKey:kNewsDetailDetailContent];
    NSMutableArray *parsedNewsDetailContent = [NSMutableArray array];
    if ([receivedNewsDetailContent isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedNewsDetailContent) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedNewsDetailContent addObject:[NewsDetailContent modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedNewsDetailContent isKindOfClass:[NSDictionary class]]) {
       [parsedNewsDetailContent addObject:[NewsDetailContent modelObjectWithDictionary:(NSDictionary *)receivedNewsDetailContent]];
    }

    self.content = [NSArray arrayWithArray:parsedNewsDetailContent];
            self.publishTime = [self objectOrNilForKey:kNewsDetailDetailPublishTime fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.detailIdentifier forKey:kNewsDetailDetailId];
    [mutableDict setValue:self.title forKey:kNewsDetailDetailTitle];
    NSMutableArray *tempArrayForContent = [NSMutableArray array];
    for (NSObject *subArrayObject in self.content) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForContent addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForContent addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForContent] forKey:kNewsDetailDetailContent];
    [mutableDict setValue:self.publishTime forKey:kNewsDetailDetailPublishTime];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.detailIdentifier = [aDecoder decodeObjectForKey:kNewsDetailDetailId];
    self.title = [aDecoder decodeObjectForKey:kNewsDetailDetailTitle];
    self.content = [aDecoder decodeObjectForKey:kNewsDetailDetailContent];
    self.publishTime = [aDecoder decodeObjectForKey:kNewsDetailDetailPublishTime];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_detailIdentifier forKey:kNewsDetailDetailId];
    [aCoder encodeObject:_title forKey:kNewsDetailDetailTitle];
    [aCoder encodeObject:_content forKey:kNewsDetailDetailContent];
    [aCoder encodeObject:_publishTime forKey:kNewsDetailDetailPublishTime];
}

- (id)copyWithZone:(NSZone *)zone
{
    NewsDetailDetail *copy = [[NewsDetailDetail alloc] init];
    
    if (copy) {

        copy.detailIdentifier = [self.detailIdentifier copyWithZone:zone];
        copy.title = [self.title copyWithZone:zone];
        copy.content = [self.content copyWithZone:zone];
        copy.publishTime = [self.publishTime copyWithZone:zone];
    }
    
    return copy;
}


@end
